package gsw.demo.mvp.core.net.service;

import java.io.File;
import java.util.HashMap;

import gsw.demo.mvp.core.net.service.callback.Callbacks;
import gsw.demo.mvp.core.net.service.callback.IError;
import gsw.demo.mvp.core.net.service.callback.IFailure;
import gsw.demo.mvp.core.net.service.callback.IRequest;
import gsw.demo.mvp.core.net.service.callback.ISuccess;
import gsw.demo.mvp.core.net.service.download.DownloadHandler;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;

/**
 * 请求体，用来设置请求参数，并调用RestService发送请求
 */
public class RestClient {
    private final HashMap<String, Object> PARAMS;
    private final String URL;
    private final IRequest REQUEST;
    private final ISuccess SUCCESS;
    private final IFailure FAILURE;
    private final IError ERROR;
    private final RequestBody BODY;
    //上传下载
    private final File FILE;
    //文件下载路径
    private final String DOWNLOAD_DIR;
    //文件扩展名
    private final String EXTENSION;
    //文件名
    private final String FILENAME;

    public RestClient(HashMap<String, Object> params, String url, IRequest request, ISuccess success, IFailure failure, IError error, RequestBody body, File file, String downloadDir, String extension, String filename) {
        this.PARAMS = params;
        this.URL = url;
        this.REQUEST = request;
        this.SUCCESS = success;
        this.FAILURE = failure;
        this.ERROR = error;
        this.BODY = body;

        this.FILE = file;
        this.DOWNLOAD_DIR = downloadDir;  ///sdcard/XXXX.ext
        this.EXTENSION = extension;
        this.FILENAME = filename;
    }

    public static RestClientBuilder create() {
        return new RestClientBuilder();
    }

    //开始实现真实的网络操作
    private void request(HttpMethod method) {
        final RestService service = RestCreator.getRestService();
        Call<String> call = null;
        if (REQUEST != null) {
            REQUEST.onRequestStart();
        }
        switch (method) {
            case GET:
                call = service.get(URL, PARAMS);
                break;
            case POST:
                call = service.post(URL, PARAMS);
                break;
            case PUT:
                call = service.put(URL, PARAMS);
                break;
            case DELETE:
                call = service.delete(URL, PARAMS);
                break;
            case UPLOAD:
                final MultipartBody.Part body = MultipartBody.Part.createFormData("file", FILE.getName());
                call = service.upload(URL, body);
                break;
            default:
                break;
        }
        if (call != null) {
            call.enqueue(getReqeustCallback());
        }
    }

    private Callback<String> getReqeustCallback() {
        return new Callbacks(REQUEST, SUCCESS, FAILURE, ERROR);
    }

    //各种请求
    public final void get() {
        request(HttpMethod.GET);
    }

    public final void post() {
        request(HttpMethod.POST);
    }

    public final void put() {
        request(HttpMethod.PUT);
    }

    public final void delete() {
        request(HttpMethod.DELETE);
    }

    public final void upload() {
        request(HttpMethod.UPLOAD);
    }

    public final void download() {
        new DownloadHandler(PARAMS, URL, REQUEST, SUCCESS, FAILURE, ERROR, DOWNLOAD_DIR, EXTENSION, FILENAME).handleDownload();
    }
}







